package zjc.bookkeeping.PhotoRecognition;

import androidx.appcompat.app.AppCompatActivity;
import androidx.percentlayout.widget.PercentRelativeLayout;

import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.wang.avi.AVLoadingIndicatorView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import zjc.bookkeeping.R;
import zjc.bookkeeping.activity.MainActivity;
import zjc.bookkeeping.api.APIManager;
import zjc.bookkeeping.db.ConsumptionBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.tools.EventInit;

public class TClassificationSelectionActivity extends AppCompatActivity {
    byte[] data ;
    List<byte[]> imgs = new ArrayList<>();
    String classname;
    AVLoadingIndicatorView  avi;
    LinearLayout avi_other;
    int method = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tclassification_selection);

        Aavi();
        initSpinner1();
    }

    @Override
    protected void onStart() {
        super.onStart();
        //注册EventBus 接收数据
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
            //onEventInit();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销事件接收
        EventBus.getDefault().unregister(this);
    }

    // 接收消息（post）
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventInit(EventInit eventInit) {
        Log.e("onevent","eee");
        try {
            method = eventInit.getMethod();
            if (eventInit.getMethod() == 0){
                data = eventInit.getImg();
                Log.e("data", String.valueOf(data));
                Log.e("img传输", String.valueOf(data));
            }else {
                imgs = eventInit.getImgs();

                //eventInit.getClass()
                Log.e("imgs传输", String.valueOf(eventInit));
                Log.e("imgs传输", String.valueOf(eventInit));
            }
            //更改TextView内容
            //tvReceiveText.setText(allValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //下拉列表
    String[] classArray1 = {"外卖小票/购物小票/电影票","火车票","出租车发票","过路费","船运票/客运票","飞机票"};
    private void initSpinner1(){
        //声明一个下拉列表的数组适配器
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,R.layout.b_item_select,classArray1);
        //设置数组适配器的布局样式
        starAdapter.setDropDownViewResource(R.layout.b_item_dropdown);
        //从布局文件中获取名叫sp_dialog的下拉框
        Spinner sp = findViewById(R.id.spinner);
        //设置下拉框的标题，不设置就没有难看的标题了
        sp.setPrompt("请选择分类");
        //设置下拉框的数组适配器
        sp.setAdapter(starAdapter);
        //设置下拉框默认的显示第一项
        sp.setSelection(0);
        //给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        sp.setOnItemSelectedListener(new MySelectedListener1());
    }
    class MySelectedListener1 implements AdapterView.OnItemSelectedListener{
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(TClassificationSelectionActivity.this,"您选择的是："+classArray1[i],Toast.LENGTH_SHORT).show();
            classname=classArray1[i];
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }
    private void  Aavi() {
        avi_other=findViewById(R.id.avi_other);
        avi=findViewById(R.id.avi);
        avi_other.setVisibility(View.VISIBLE);
        avi.hide();
    }
    public void sureadd(View v){
        avi_other.setVisibility(View.GONE);
        avi.show();
        if (classname.equals("外卖小票/购物小票/电影票")){
            Log.i("2.data：", String.valueOf(data));
            if (method == 0) {
                new Thread() {
                    @Override
                    public void run() {
                        String result = APIManager.ShopTicket(data);
                        Log.i("系统相册处理结果", result);
                        Intent intent = new Intent(TClassificationSelectionActivity.this, addPhotoConsumptionActivity.class);
                        intent.putExtra("values", result);
                        intent.putExtra("billtype", "ShopTicket");
                        startActivity(intent);
                    }
                }.start();
            }else {
                Log.e("多选","zhi");
                ExecutorService pool = Executors.newFixedThreadPool(10);
                Intent intent = new Intent(TClassificationSelectionActivity.this, MainActivity.class);
                Log.e("imgss",String.valueOf(imgs.size()));
                        for (int j = 0; j < imgs.size(); j++) {
                            Log.e("imgs","zhi");
                            int finalJ = j;
                            pool.submit(new Runnable() {
                                @Override
                                public void run() {
                                    System.out.println("hello world! Execute ThreadName=" + Thread.currentThread().getName());

                            String result = APIManager.ShopTicket(imgs.get(finalJ));
                            Log.i("系统相册处理结果1", result);
                            //intent.putExtra("values", result);
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                String item_list = jsonObject.getJSONObject("result").getString("item_list");
                                JSONArray jsonarray = new JSONArray(item_list);
                                ConsumptionBean consumptionBean = new ConsumptionBean();
                                consumptionBean.setConsumption_class_name("餐饮");
                                consumptionBean.setaccount_id(DBManager.selectAccountId() + "");
                                consumptionBean.setConsumption_method("现金");
                                for (int i = 0; i < jsonarray.length(); i++) {
                                    //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                                    String description = jsonobject.optString("description");
                                    String value = jsonobject.optString("value");
                                    if (description.equals("金额")) {
                                        String numStr = "";
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[\\d.]");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                            }
                                        }
                                        Log.e("金额", numStr);
                                        float nums = Float.valueOf(numStr);
                                        consumptionBean.setConsumption_money(nums);
                                        Log.i("金额", numStr);
                                    } else if (description.equals("时间")) {
                                        Log.i("时间", value);
                                        String[] timei = new String[5];
                                        String numStr = "";
                                        int k = 0;
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[0-9]+");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                            } else {
                                                timei[k] = numStr;
                                                k++;
                                                numStr = "";
                                            }
                                        }
                                        String time = timei[0] + "年" + timei[1] + "月" + timei[2] + "日 " + timei[3] + ":" + timei[4];
                                        Log.i("time", time);
                                        consumptionBean.setConsumption_time(time);
                                        consumptionBean.setConsumption_year(Integer.parseInt(timei[0]));
                                        consumptionBean.setConsumption_month(Integer.parseInt(timei[1]));
                                        consumptionBean.setConsumption_day(Integer.parseInt(timei[3]));
                                    } else if (description.equals("商户")) {
                                        consumptionBean.setConsumption_business(value);
                                        Log.i("商户", value);
                                    } else if (description.equals("商品")) {
                                        consumptionBean.setConsumption_remarks(value);
                                        Log.i("商品", value);
                                    } else {
                                        Log.i("问题", "都不对");
                                    }
                                }
                                DBManager.insertConsumption(consumptionBean);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                                }
                            });
                        }
                startActivity(intent);

            }
        }else  if (classname.equals("火车票")){
            Log.i("2.data：", String.valueOf(data));
            if (method == 0) {
            new Thread() {
                @Override
                public void run() {
                    String result = APIManager.TrainTickets(data);
                    Log.i("系统相册处理结果", result);
                    Intent intent = new Intent(TClassificationSelectionActivity.this, addPhotoConsumptionActivity.class);
                    intent.putExtra("values", result);
                    intent.putExtra("billtype","TrainTickets");
                    startActivity(intent);
                }
            }.start();

            }else {new Thread() {
                @Override
                public void run() {
                for (int j = 0; j < imgs.size(); j++) {
                    int finalI = j;
                            Intent intent = new Intent(TClassificationSelectionActivity.this, MainActivity.class);
                            String result = APIManager.TrainTickets(imgs.get(finalI));
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                ConsumptionBean consumptionBean = new ConsumptionBean();
                                consumptionBean.setConsumption_class_name("出行");
                                consumptionBean.setaccount_id(DBManager.selectAccountId() + "");
                                consumptionBean.setConsumption_method("现金");
                                String item_list = jsonObject.getJSONObject("result").getString("item_list");
                                JSONArray jsonarray = new JSONArray(item_list);
                                String s = "";
                                for(int i = 0 ;i<jsonarray.length();i++) {
                                    //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                                    String description = jsonobject.optString("description");
                                    String value = jsonobject.optString("value");
                                    if (description.equals("价格")) {
                                        String numStr = "";
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[\\d.]");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                            }
                                        }
                                        float nums = Float.valueOf(numStr);
                                        consumptionBean.setConsumption_money(nums);
                                        Log.i("金额", numStr);
                                    } else if (description.equals("乘车时间")) {
                                        Log.i("乘车时间", value);
                                        String[] timei = new String[5];
                                        String numStr = "";
                                        int k = 0;
                                        Log.i("value.length()", String.valueOf(value.length()));
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[0-9]+");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                                Log.i(n + ".numStr:", numStr);
                                            } else {
                                                timei[k] = numStr;
                                                Log.i("numStr" + k + ":", numStr);
                                                k++;
                                                numStr = "";
                                            }
                                            timei[4] = numStr;
                                        }
                                        String time = timei[0] + "年" + timei[1] + "月" + timei[2] + "日 " + timei[3] + ":" + timei[4];
                                        Log.i("time", time);
                                        if(time.contains("null")){
                                            Log.i("时间不对","不对");
                                            Date date = new Date();
                                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                            Calendar calendar = Calendar.getInstance();
                                            consumptionBean.setConsumption_year(calendar.get(Calendar.YEAR));
                                            consumptionBean.setConsumption_month(calendar.get(Calendar.MONTH) + 1);
                                            consumptionBean.setConsumption_day(calendar.get(Calendar.DAY_OF_MONTH));
                                            consumptionBean.setConsumption_time(String.valueOf(calendar.get(Calendar.YEAR))+"年"+String.valueOf((calendar.get(Calendar.MONTH)+1))+"月"+String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)+"日"));
                                        }else {
                                            consumptionBean.setConsumption_time(time);
                                            consumptionBean.setConsumption_year(Integer.parseInt(timei[0]));
                                            consumptionBean.setConsumption_month(Integer.parseInt(timei[1]));
                                            consumptionBean.setConsumption_day(Integer.parseInt(timei[3]));
                                        }
                                    } else if (description.equals("出发地")) {
                                        s = value + " - ";
                                        Log.i("出发地", value);
                                    } else if (description.equals("车次号")) {
                                        s += value + " - ";
                                        Log.i("车次号", value);
                                    } else if (description.equals("目的地")) {
                                        s += value;
                                        consumptionBean.setConsumption_remarks(s);
                                        Log.i("目的地", value);
                                    } else if (description.equals("检票口")) {
                                        consumptionBean.setConsumption_business(value);
                                        Log.i("检票口", value);
                                    } else {
                                        Log.i("问题", "都不对");
                                    }
                                }
                                DBManager.insertConsumption(consumptionBean);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            startActivity(intent);
                        }
                }
                    }.start();

                    Log.e("火车票", "多选");

            }
        }else  if (classname.equals("出租车发票")){
            Log.i("2.data：", String.valueOf(data));
            if (method == 0) {
                new Thread() {
                    @Override
                    public void run() {
                        String result = APIManager.TaxiInvoice(data);
                        Log.i("系统相册处理结果", result);
                        Intent intent = new Intent(TClassificationSelectionActivity.this, addPhotoConsumptionActivity.class);
                        intent.putExtra("values", result);
                        intent.putExtra("billtype", "TaxiInvoice");
                        startActivity(intent);
                    }
                }.start();
            }else {
                ExecutorService pool = Executors.newFixedThreadPool(10);
                Intent intent = new Intent(TClassificationSelectionActivity.this, MainActivity.class);
                for (int j = 0; j < imgs.size(); j++) {
                    int finalJ = j;
                    pool.submit(new Runnable() {
                        @Override
                        public void run() {
                            String result = APIManager.TaxiInvoice(imgs.get(finalJ));
                            String s = "";
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                String item_list = jsonObject.getJSONObject("result").getString("item_list");
                                JSONArray jsonarray = new JSONArray(item_list);
                                ConsumptionBean consumptionBean = new ConsumptionBean();
                                consumptionBean.setConsumption_class_name("出行");
                                consumptionBean.setaccount_id(DBManager.selectAccountId() + "");
                                consumptionBean.setConsumption_method("现金");
                                for (int i = 0; i < jsonarray.length(); i++) {
                                    //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                                    String description = jsonobject.optString("description");
                                    String value = jsonobject.optString("value");
                                    if (description.equals("金额")) {
                                        String numStr = "";
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[\\d.]");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                            }
                                        }
                                        float nums = Float.valueOf(numStr);
                                        consumptionBean.setConsumption_money(nums);
                                        Log.i("金额", numStr);
                                    } else if (description.equals("日期")) {
                                        Log.i("时间", value);
                                        String[] timei = new String[5];
                                        String numStr = "";
                                        int k = 0;
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[0-9]+");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                            } else {
                                                timei[k] = numStr;
                                                k++;
                                                numStr = "";
                                            }
                                        }
                                        String time = timei[0] + "年" + timei[1] + "月" + timei[2] + "日 " + timei[3] + ":" + timei[4];
                                        Log.i("time", time);
                                        if(time.contains("null")){
                                            Log.i("时间不对","不对");
                                            Date date = new Date();
                                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                            Calendar calendar = Calendar.getInstance();
                                            consumptionBean.setConsumption_year(calendar.get(Calendar.YEAR));
                                            consumptionBean.setConsumption_month(calendar.get(Calendar.MONTH) + 1);
                                            consumptionBean.setConsumption_day(calendar.get(Calendar.DAY_OF_MONTH));
                                            consumptionBean.setConsumption_time(String.valueOf(calendar.get(Calendar.YEAR))+"年"+String.valueOf((calendar.get(Calendar.MONTH)+1))+"月"+String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)+"日"));
                                        }else {
                                            consumptionBean.setConsumption_time(time);
                                            consumptionBean.setConsumption_year(Integer.parseInt(timei[0]));
                                            consumptionBean.setConsumption_month(Integer.parseInt(timei[1]));
                                            consumptionBean.setConsumption_day(Integer.parseInt(timei[3]));
                                        }
                                    }else if(description.equals("车号")){
                                        s = value+" 里程";
                                        Log.i("车号",value);
                                    }else if(description.equals("里程")){
                                        s += value;
                                        consumptionBean.setConsumption_remarks(s);
                                        Log.i("里程",value);
                                    } else {
                                        Log.i("问题", "都不对");
                                    }
                                }
                                DBManager.insertConsumption(consumptionBean);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                startActivity(intent);

            }
        }else  if (classname.equals("过路费")) {
            if (method == 0) {
                new Thread() {
                    @Override
                    public void run() {
                        String result = APIManager.RoadToll(data);
                        Log.i("系统相册处理结果", result);
                        Intent intent = new Intent(TClassificationSelectionActivity.this, addPhotoConsumptionActivity.class);
                        intent.putExtra("values", result);
                        intent.putExtra("billtype", "RoadToll");
                        startActivity(intent);
                    }
                }.start();
            }else {
                ExecutorService pool = Executors.newFixedThreadPool(10);
                Intent intent = new Intent(TClassificationSelectionActivity.this, MainActivity.class);
                for (int j = 0; j < imgs.size(); j++) {
                    int finalJ = j;
                    pool.submit(new Runnable() {
                        @Override
                        public void run() {
                            String result = APIManager.RoadToll(imgs.get(finalJ));
                            String s = "";
                            try {
                                String[] timei = new String[5];
                                String time = "";
                                JSONObject jsonObject = new JSONObject(result);
                                String item_list = jsonObject.getJSONObject("result").getString("item_list");
                                JSONArray jsonarray = new JSONArray(item_list);
                                ConsumptionBean consumptionBean = new ConsumptionBean();
                                consumptionBean.setConsumption_class_name("出行");
                                consumptionBean.setaccount_id(DBManager.selectAccountId() + "");
                                consumptionBean.setConsumption_method("现金");
                                for (int i = 0; i < jsonarray.length(); i++) {
                                    //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                                    String description = jsonobject.optString("description");
                                    String value = jsonobject.optString("value");
                                    if (description.equals("收费金额")) {
                                        String numStr = "";
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[\\d.]");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                            }
                                        }
                                        float nums = Float.valueOf(numStr);
                                        consumptionBean.setConsumption_money(nums);
                                        Log.i("金额", numStr);
                                        if(numStr==""){
                                            Toast.makeText(getApplicationContext(), "金额为空,请添加金额", Toast.LENGTH_LONG).show();
                                            continue;
                                        }
                                        Log.i("金额",numStr);
                                    } else if (description.equals("收费日期")) {
                                        Log.i("时间", value);
                                        String numStr = "";
                                        int k = 0;
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[0-9]+");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                            } else {
                                                timei[k] = numStr;
                                                k++;
                                                numStr = "";
                                            }
                                        }
                                       time = timei[0] + "年" + timei[1] + "月" + timei[2] + "日 " + timei[3] + ":" + timei[4];
                                        if(time.contains("null")){
                                            Log.i("时间不对","不对");
                                            Date date = new Date();
                                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                            String time1 = simpleDateFormat.format(date);
                                            Calendar calendar = Calendar.getInstance();
                                            String year = String.valueOf(calendar.get(Calendar.YEAR));
                                            String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
                                            String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
                                        }
                                        Log.i("time", time);
                                    }else if(description.equals("收费时间")){
                                        Log.i("收费时间",value);
                                        String numStr = "";
                                        int k=3;
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n+1);
                                            Pattern pattern = Pattern.compile("[0-9]+");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr +=subStr;
                                            }else {
                                                timei[k]=numStr;
                                                k++;
                                                numStr="";
                                            }
                                        }
                                        time += timei[3]+":"+timei[4] ;
                                        Log.i("time",time);
                                        if(time.contains("null")){
                                            Log.i("时间不对","不对");
                                            Date date = new Date();
                                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                            Calendar calendar = Calendar.getInstance();
                                            consumptionBean.setConsumption_year(calendar.get(Calendar.YEAR));
                                            consumptionBean.setConsumption_month(calendar.get(Calendar.MONTH) + 1);
                                            consumptionBean.setConsumption_day(calendar.get(Calendar.DAY_OF_MONTH));
                                            consumptionBean.setConsumption_time(String.valueOf(calendar.get(Calendar.YEAR))+"年"+String.valueOf((calendar.get(Calendar.MONTH)+1))+"月"+String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)+"日"));
                                        }else {
                                            consumptionBean.setConsumption_time(time);
                                            consumptionBean.setConsumption_year(Integer.parseInt(timei[0]));
                                            consumptionBean.setConsumption_month(Integer.parseInt(timei[1]));
                                            consumptionBean.setConsumption_day(Integer.parseInt(timei[3]));
                                        }
                                    }else if(description.equals("发票所在地")){
                                        s=value+" ";
                                        Log.i("发票所在地",value);
                                    }else if(description.equals("车辆通行费代码")){
                                        s+=value;
                                        consumptionBean.setConsumption_remarks(s);
                                        Log.i("车辆通行费代码",value);
                                    } else {
                                        Log.i("问题", "都不对");
                                    }
                                }
                                DBManager.insertConsumption(consumptionBean);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                startActivity(intent);

            }
        }else  if (classname.equals("船运票/客运票")){
            if (method == 0) {
                Log.i("2.data：", String.valueOf(data));
                new Thread() {
                    @Override
                    public void run() {
                        String result = APIManager.PassengerTicket(data);
                        Log.i("系统相册处理结果", result);
                        Intent intent = new Intent(TClassificationSelectionActivity.this, addPhotoConsumptionActivity.class);
                        intent.putExtra("values", result);
                        intent.putExtra("billtype", "PassengerTicket");
                        startActivity(intent);
                    }
                }.start();
            }else {
                ExecutorService pool = Executors.newFixedThreadPool(10);
                Intent intent = new Intent(TClassificationSelectionActivity.this, MainActivity.class);
                for (int j = 0; j < imgs.size(); j++) {
                    int finalJ = j;
                    pool.submit(new Runnable() {
                        @Override
                        public void run() {
                            String result = APIManager.PassengerTicket(imgs.get(finalJ));
                            String s = "";
                            try {
                                String[] timei = new String[5];
                                String time = "";
                                JSONObject jsonObject = new JSONObject(result);
                                String item_list = jsonObject.getJSONObject("result").getString("item_list");
                                JSONArray jsonarray = new JSONArray(item_list);
                                ConsumptionBean consumptionBean = new ConsumptionBean();
                                consumptionBean.setConsumption_class_name("出行");
                                consumptionBean.setaccount_id(DBManager.selectAccountId() + "");
                                consumptionBean.setConsumption_method("现金");
                                for (int i = 0; i < jsonarray.length(); i++) {
                                    //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                                    String description = jsonobject.optString("description");
                                    String value = jsonobject.optString("value");
                                    if (description.equals("金额")) {
                                        String numStr = "";
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[\\d.]");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                            }
                                        }
                                        float nums = Float.valueOf(numStr);
                                        consumptionBean.setConsumption_money(nums);
                                        Log.i("金额", numStr);
                                        if(numStr==""){
                                            Toast.makeText(getApplicationContext(), "金额为空,请添加金额", Toast.LENGTH_LONG).show();
                                            continue;
                                        }
                                        Log.i("金额",numStr);
                                    } else if (description.equals("日期")) {
                                        String numStr = "";
                                        int k=0;
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n+1);
                                            Pattern pattern = Pattern.compile("[0-9]+");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr +=subStr;
                                            }else {
                                                timei[k]=numStr;
                                                k++;
                                                numStr="";
                                            }
                                            timei[2]=numStr;
                                        }
                                        time = timei[0]+"年"+timei[1]+"月"+timei[2]+"日 ";
                                        Log.i("time", time);
                                    }else if(description.equals("时间")){
                                        Log.i("时间",value);
                                        String numStr = "";
                                        int k=3;
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n+1);
                                            Pattern pattern = Pattern.compile("[0-9]+");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr +=subStr;
                                            }else {
                                                timei[k]=numStr;
                                                k++;
                                                numStr="";
                                            }
                                        }
                                        time += timei[3]+":"+timei[4] ;
                                        if(time.contains("null")){
                                            Log.i("时间不对","不对");
                                            Date date = new Date();
                                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                            Calendar calendar = Calendar.getInstance();
                                            consumptionBean.setConsumption_year(calendar.get(Calendar.YEAR));
                                            consumptionBean.setConsumption_month(calendar.get(Calendar.MONTH) + 1);
                                            consumptionBean.setConsumption_day(calendar.get(Calendar.DAY_OF_MONTH));
                                            consumptionBean.setConsumption_time(String.valueOf(calendar.get(Calendar.YEAR))+"年"+String.valueOf((calendar.get(Calendar.MONTH)+1))+"月"+String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)+"日"));
                                        }else {
                                            consumptionBean.setConsumption_time(time);
                                            consumptionBean.setConsumption_year(Integer.parseInt(timei[0]));
                                            consumptionBean.setConsumption_month(Integer.parseInt(timei[1]));
                                            consumptionBean.setConsumption_day(Integer.parseInt(timei[3]));
                                        }
                                        Log.i("time",time);
                                       // consumptionBean.setConsumption_time(time);
                                    }else if(description.equals("发票号码")){
                                        consumptionBean.setConsumption_remarks(value);
                                        Log.i("发票号码",value);
                                    }else if(description.equals("发票代码")){
                                        consumptionBean.setConsumption_business(value);
                                        Log.i("发票代码",value);
                                    } else {
                                        Log.i("问题", "都不对");
                                    }
                                }
                                DBManager.insertConsumption(consumptionBean);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                startActivity(intent);
            }
        }else  if (classname.equals("飞机票")){
            Log.i("2.data：", String.valueOf(data));
            if (method == 0) {
                new Thread() {
                    @Override
                    public void run() {
                        String result = APIManager.PlaneTicket(data);
                        Log.i("系统相册处理结果", result);
                        Intent intent = new Intent(TClassificationSelectionActivity.this, addPhotoConsumptionActivity.class);
                        intent.putExtra("values", result);
                        intent.putExtra("billtype", "PlaneTicket");
                        startActivity(intent);
                    }
                }.start();
            }else {
                ExecutorService pool = Executors.newFixedThreadPool(10);
                Intent intent = new Intent(TClassificationSelectionActivity.this, MainActivity.class);
                for (int j = 0; j < imgs.size(); j++) {
                    int finalJ = j;
                    pool.submit(new Runnable() {
                        @Override
                        public void run() {
                            String result = APIManager.PlaneTicket(imgs.get(finalJ));
                            String s = "";
                            try {
                                String[] timei = new String[5];
                                String time = "";
                                JSONObject jsonObject = new JSONObject(result);
                                String item_list = jsonObject.getJSONObject("result").getString("item_list");
                                JSONArray jsonarray = new JSONArray(item_list);
                                ConsumptionBean consumptionBean = new ConsumptionBean();
                                consumptionBean.setConsumption_class_name("出行");
                                consumptionBean.setaccount_id(DBManager.selectAccountId() + "");
                                consumptionBean.setConsumption_method("现金");
                                for (int i = 0; i < jsonarray.length(); i++) {
                                    //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                                    String description = jsonobject.optString("description");
                                    String value = jsonobject.optString("value");
                                    if (description.equals("合计金额")) {
                                        String numStr = "";
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n + 1);
                                            Pattern pattern = Pattern.compile("[\\d.]");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr += subStr;
                                            }
                                        }
                                        float nums = Float.valueOf(numStr);
                                        consumptionBean.setConsumption_money(nums);
                                        Log.i("金额", numStr);
                                        if(numStr==""){
                                            Toast.makeText(getApplicationContext(), "金额为空,请添加金额", Toast.LENGTH_LONG).show();
                                            continue;
                                        }
                                        Log.i("金额",numStr);
                                    } else if (description.equals("填开日期")) {
                                        Log.i("时间", value);
                                        String numStr = "";
                                        int k=0;
                                        for (int n = 0; n < value.length(); n++) {
                                            String subStr = value.substring(n, n+1);
                                            Pattern pattern = Pattern.compile("[0-9]+");
                                            Matcher matcher = pattern.matcher(subStr);
                                            if (matcher.find()) {
                                                numStr +=subStr;
                                            }else {
                                                timei[k]=numStr;
                                                k++;
                                                numStr="";
                                            }
                                            timei[2]=numStr;
                                        }
                                        time = timei[0]+"年"+timei[1]+"月"+timei[2]+"日";
                                        if(time.contains("null")){
                                            Log.i("时间不对","不对");
                                            Date date = new Date();
                                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                            Calendar calendar = Calendar.getInstance();
                                            consumptionBean.setConsumption_year(calendar.get(Calendar.YEAR));
                                            consumptionBean.setConsumption_month(calendar.get(Calendar.MONTH) + 1);
                                            consumptionBean.setConsumption_day(calendar.get(Calendar.DAY_OF_MONTH));
                                            consumptionBean.setConsumption_time(String.valueOf(calendar.get(Calendar.YEAR))+"年"+String.valueOf((calendar.get(Calendar.MONTH)+1))+"月"+String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)+"日"));
                                        }else {
                                            consumptionBean.setConsumption_time(time);
                                            consumptionBean.setConsumption_year(Integer.parseInt(timei[0]));
                                            consumptionBean.setConsumption_month(Integer.parseInt(timei[1]));
                                            consumptionBean.setConsumption_day(Integer.parseInt(timei[3]));
                                        }
                                    }else if(description.equals("电子客票号码")){
                                        consumptionBean.setConsumption_remarks(value);
                                        Log.i("电子客票号码",value);
                                    }else if(description.equals("填开单位")){
                                        consumptionBean.setConsumption_business(value);
                                        Log.i("填开单位",value);
                                    } else {
                                        Log.i("问题", "都不对");
                                    }
                                }
                                DBManager.insertConsumption(consumptionBean);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                startActivity(intent);
            }
        }
    }

//
//    public static ExecutorService newFixThreadPool(int nThreads){
//        return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
//    }
////使用
//Executors.newFixThreadPool(5).execute(r);
}